package heven.holt.wan.android.fragment

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import com.tencent.smtt.sdk.WebView
import dagger.hilt.android.AndroidEntryPoint
import heven.holt.library.base.BaseFragment
import heven.holt.library.http.webview.OnPageChangedListener
import heven.holt.library.http.webview.WebViewHelper
import heven.holt.wan.android.databinding.FragmentWebViewBinding
import heven.holt.wan.android.viewmodel.WebViewModel

@AndroidEntryPoint
class WebViewFragment : BaseFragment<FragmentWebViewBinding>() {
    private val args: WebViewFragmentArgs by navArgs()

    private val webViewModel: WebViewModel by viewModels()

    private lateinit var webViewHelper: WebViewHelper

    override fun bindViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentWebViewBinding = FragmentWebViewBinding.inflate(inflater, container, false)

    override fun initView() {
        webViewHelper = WebViewHelper.with(binding.webContainer)
            .setOnPageChangedListener(object : OnPageChangedListener {
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    webViewModel.isLoadingLiveData.postValue(true)
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    webViewModel.isLoadingLiveData.postValue(false)
                }

                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    webViewModel.progressLiveData.postValue(newProgress)
                }
            })
        webViewHelper.loadUrl(Uri.decode(args.url))

        val onBackPressed =
            requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
                if (!webViewHelper.canGoBack()) {
                    this.isEnabled = false
                    requireActivity().onBackPressed()
                }
            }
        binding.black.setOnClickListener {
            if (!webViewHelper.canGoBack()) {
                onBackPressed.isEnabled = false
                requireActivity().onBackPressed()
            }
        }
        binding.forward.setOnClickListener {
            webViewHelper.canGoForward()
        }
        binding.refresh.setOnClickListener {
            webViewHelper.reload()
        }
        binding.browse.setOnClickListener {
            try {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(args.url))
                intent.addCategory(Intent.CATEGORY_BROWSABLE)
                requireActivity().startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun initData() {
        binding.lifecycleOwner = viewLifecycleOwner
        binding.isLoading = webViewModel.isLoadingLiveData
        binding.progress = webViewModel.progressLiveData
    }

    override fun onResume() {
        webViewHelper.onResume()
        super.onResume()
    }

    override fun onPause() {
        webViewHelper.onPause()
        super.onPause()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        webViewHelper.onDestroyView()
    }
}